<template>
  <div class="app-container">

    <div class="search-div">
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="工厂类型">
              <el-select v-model="searchObj.fType" placeholder="工厂类型">
                <el-option label="家电" :value="1"></el-option>
                <el-option label="百货" :value="2"></el-option>
                <el-option label="食品" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工厂名字">
              <el-input style="width: 95%" v-model="searchObj.fname" placeholder="工厂名字"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading" @click="fetchData()">搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
          <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;">

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      @selection-change="handleSelectionChange" >

      <el-table-column type="selection"/>

      <el-table-column label="图片">
        <div style="display: flex; justify-content: center; align-items: center; height: 100%;">
          <img src="/images/shopMall.png" alt="店铺" style="max-width: 100%; max-height: 100%;"/>
        </div>
      </el-table-column>
      <el-table-column prop="fname" label="工厂名"/>
      <el-table-column prop="roleName" label="工厂种类"/>

      <el-table-column label="操作" width="250" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="toBuygoods(scope.row)">
            购买商品
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />


  </div>
</template>

<script>
import api from '@/api/system/sysFactory'

const defaultForm = {
  Id: '',
  fname: '',
  fType: '',
  dcount: '',
  fImage: '',
  roleName: ''

}
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      createTimes: [],

      dialogVisible: false,
      Factory: defaultForm,
      saveBtnDisabled: false,
      dialogRoleVisible: false,
      isIndeterminate: false, // 是否是不确定的
      checkAll: false // 是否全选

    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')

    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },
    handleSelectionChange(selection) {
      this.selections = selection
      console.log(this.selections)
    },

    // 加载banner列表数据
    fetchData(page = 1) {
      this.page = page
      console.log('翻页。。。' + this.page)
      console.log(this.fType)
      api.getPageList(this.page, this.limit, this.searchObj).then(
        response => {
          // this.list = response.data.list
          this.list = response.data.records
          this.total = response.data.total

          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(Id) {
      // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        return api.removeById(Id)
      }).then((response) => {
        this.fetchData(this.page)
        this.$message.success(response.message || '删除成功')
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },

    // -------------
    add() {
      this.dialogVisible = true
      this.Factory = Object.assign({}, defaultForm)
    },

    edit(Id) {
      this.dialogVisible = true
      this.fetchDataById(Id)
    },

    fetchDataById(Id) {
      api.getById(Id).then(response => {
        this.Factory = response.data
      })
    },

    saveOrUpdate() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.saveBtnDisabled = true // 防止表单重复提交
          if (!this.Factory.Id) {
            this.saveData()
          } else {
            this.updateData()
          }
        }
      })
    },

    // 新增
    saveData() {
      api.save(this.Factory).then(response => {
        this.$message.success('操作成功')
        this.dialogVisible = false
        this.fetchData(this.page)
      })
    },

    // 根据id更新记录
    updateData() {
      api.updateById(this.Factory).then(response => {
        this.$message.success(response.message || '操作成功')
        this.dialogVisible = false
        this.fetchData(this.page)
      })
    },

    showAssignRole(row) {
      this.Factory = row
      this.dialogRoleVisible = true
      this.getRoles()
    },
    toBuygoods(row) {
      var fType = row.ftype

      this.$router.push({ name: 'shopMall', params: { fType } })
    }
  }
}
</script>
